package com.zhouyu;

import com.zhouyu.aop.ZhouyuAfterReturningAdvice;
import com.zhouyu.thirteen.UserService;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.*;

@ComponentScan("com.zhouyu.thirteen")
@Configuration
@Import(DefaultAdvisorAutoProxyCreator.class)
//注意 这个注解要加，才能使aspectJ的aop配置生效
@EnableAspectJAutoProxy
public class AppConfig13 {

	@Bean
	public DefaultPointcutAdvisor defaultPointcutAdvisor(){
		NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
		pointcut.addMethodName("test");

		DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
		defaultPointcutAdvisor.setPointcut(pointcut);
		defaultPointcutAdvisor.setAdvice(new ZhouyuAfterReturningAdvice());

		return defaultPointcutAdvisor;
	}

//	@Bean  注解的那一块跟这部分逻辑一致
//	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//		DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
//		return defaultAdvisorAutoProxyCreator;
//	}

//	@Bean
//	public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
//		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
//		beanNameAutoProxyCreator.setBeanNames("userSe*");
//		beanNameAutoProxyCreator.setInterceptorNames("zhouyuAroundAdvice");
//		beanNameAutoProxyCreator.setProxyTargetClass(true);
//
//		return beanNameAutoProxyCreator;
//	}

//	@Bean
//	public MethodInterceptor zhouyuAroundAdvice(){
//		return new MethodInterceptor() {
//			@Override
//			public Object invoke(MethodInvocation invocation) throws Throwable {
//				System.out.println("before...");
//				Object result = invocation.proceed();
//				System.out.println("after...");
//				return result;
//			}
//		};
//	}
//
//	@Bean
//	public ProxyFactoryBean userService(){
//		UserService userService = new UserService();
//
//		ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
//		proxyFactoryBean.setTarget(userService);
//		proxyFactoryBean.setInterceptorNames("zhouyuAroundAdvice");
//		return proxyFactoryBean;
//	}



	@Bean
	public ProxyFactoryBean userServiceProxy(){
		UserService userService = new UserService();

		ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
		proxyFactoryBean.setTarget(userService);

		proxyFactoryBean.addAdvice(new MethodInterceptor() {
			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("before...");
				Object result = invocation.proceed();
				System.out.println("after...");
				return result;
			}
		});
		return proxyFactoryBean;
	}



}
